<template>
	<el-dialog title="新增分类类型枚举" :visible.sync="visible" width="560px" top="0" append-to-body>
		<el-form ref="form" size="medium" :model="enumFrom" :rules="enumRules" label-width="80px" class="enum_form_wrapper">
			<el-row :gutter="16">
				<el-col :span="24">
					<el-form-item label="枚举名称" prop="deName">
						<el-input v-model="enumFrom.deName" placeholder="请输入枚举名称"></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="枚举值" prop="deValue">
						<sw-custom-input type="enums" v-model="enumFrom.deValue" placeholder="请输入枚举值" />
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="枚举描述">
						<el-input type="textarea" v-model="enumFrom.content"></el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template slot="footer">
			<el-button size="medium" @click="visible = false">取消</el-button>
			<el-button size="medium" type="primary" :loading="btnLoading" @click="handleSubmitForm">新增</el-button>
		</template>
	</el-dialog>
</template>

<script>
import { addedDictDetail } from '@/api/system/tbSysDictDetail';
export default {
	props: {
		value: { type: Boolean, default: false },
		dictId: { type: String, default: '' }
	},
	data() {
		const validateEnumName = (rule, value, callback) => {
			let classifys = this.dictData['SYS_CLASSIFY'];
			if (!value) {
				callback(new Error('请输入枚举名称'));
			} else if (classifys.some((n) => n.deName === value)) {
				callback(new Error('枚举名称已存在'));
			} else {
				callback();
			}
		};
		const validateEnumValue = (rule, value, callback) => {
			let classifys = this.dictData['SYS_CLASSIFY'];
			if (!value) {
				callback(new Error('请输入枚举值'));
			} else if (classifys.some((n) => n.deValue === value)) {
				callback(new Error('枚举值已存在'));
			} else {
				callback();
			}
		};
		return {
			visible: this.value,
			enumFrom: {},
			enumRules: {
				deName: [{ required: true, validator: validateEnumName, trigger: 'blur' }],
				deValue: [{ required: true, validator: validateEnumValue, trigger: 'blur' }]
			},
			btnLoading: false
		};
	},
	watch: {
		value(flag) {
			this.visible = flag;
		},
		visible(flag) {
			this.$emit('input', flag);
			if (!flag) return false;
			this.$nextTick(() => {
				this.$refs['form'].resetFields();
				this.enumFrom = {};
			});
		}
	},
	methods: {
		handleSubmitForm() {
			this.$refs['form'].validate((valid) => {
				if (!valid) return false;
				this.btnLoading = true;
				this.submitFormRequest();
			});
		},

		//添加/修改枚举请求
		async submitFormRequest() {
			let params = { ...this.enumFrom, dictId: this.dictId };
			let result = await addedDictDetail(params).then((res) => res.data);
			this.btnLoading = false;
			if (result.code === '0') {
				this.$message.success(`新增枚举成功`);
				this.visible = false;
				this.$emit('finish');
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.enum_form_wrapper::v-deep {
	width: 100%;
	padding: 10px 6px 0 0;
	box-sizing: border-box;
	.el-input-number {
		width: 100%;
	}
}
</style>
